/*
 * CollectionDao.java
 *
 * Created on 4 de abril de 2006, 22:28
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import fmap.DatosCompartidos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import fmap.model.Cat;
import fmap.utils.IConstants;

/**
 *
 * @author javi
 */
public class CatDao
{
    
    /** Creates a new instance of CollectionDao */
    public CatDao ()
    {
    }
    

    public Vector selectCats ()
    {
        Vector vCats = new Vector();
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idCat, nameCat FROM fmap.cat ");
            
            ResultSet rs = ps.executeQuery ();

            while( rs.next () )
            {
                vCats.add ( new Cat(rs.getInt (1), rs.getString (2)) );
            }

            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return vCats;
    }
    
    
    public Cat selectCat ( int idCat )
    {
        Cat cat = null;

        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idCat, nameCat FROM fmap.cat WHERE idCat=?");

            ps.setInt (1, idCat);

            ResultSet rs = ps.executeQuery ();

            if( rs.next () )
            {
                cat = new Cat(rs.getInt (1), rs.getString (2));
            }

            rs.close ();
            ps.close ();
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return cat;
    }
    
    
    public boolean insertCat ( String cat )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "INSERT INTO fmap.cat ( nameCat ) VALUES(?)");
            
            ps.setString (1, cat);
            
            ps.executeUpdate ();
            
            ps.close ();

        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
            return false;
        }
        
        return true;
    }
    
    
    public boolean deleteCat ( int idCat )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.cat WHERE idCat=?");
            ps.setInt ( 1, idCat );
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }

        return true;
    }

}